package com.ijunfu.threadpool;

import com.google.common.util.concurrent.ThreadFactoryBuilder;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.*;
import java.util.stream.IntStream;

/**
 *
 * @title  : 使用Guava创建线程池，并自定义线程名称
 * @author : ijunfu <ijunfu@163.com>
 * @date   : 2023/10/23 16:20
 * @version: 1.0
 * @motto  : 位来可期 - 世界很大，我想看看
 *
 */
@Slf4j
public class GuavaThreadPool {

    public static void main(String[] args) {

        // 1. 定义ThreadFactory
        ThreadFactory namedThreadFactory = new ThreadFactoryBuilder().setNameFormat("ijunfu-%d").build();

        // 2. 创建线程池
        ExecutorService pool =
                new ThreadPoolExecutor(
                        5,
                        100,
                        0L,
                        TimeUnit.SECONDS,
                        new LinkedBlockingQueue<>(2048),
                        namedThreadFactory,
                        new ThreadPoolExecutor.AbortPolicy());

        // 3. 业务执行
        IntStream.range(0, 10).forEach(e -> pool.execute(() -> log.info("{}", e)));


        // 4. 销毁线程池
        pool.shutdown();
    }

}
